<?php

require_once 'config.php';

require_once 'includes/functions.php';

$searchKeyword = isset($_GET['q']) ? trim($_GET['q']) : '';

$apps = getApps();

$searchResults = [];

if (!empty($searchKeyword)) {

    foreach ($apps as $app) {

        // 搜索应用名称、介绍、更新内容

        if (stripos($app['name'], $searchKeyword) !== false ||

            stripos($app['description'], $searchKeyword) !== false ||

            stripos($app['update_content'], $searchKeyword) !== false) {

            $searchResults[] = $app;

        }

    }

}

?>

<?php include 'includes/header.php'; ?>

<!-- 搜索框 -->

<div style="background: white; padding: 30px; border-radius: 10px; margin-bottom: 30px; text-align: center;">

    <h2 style="margin-bottom: 20px;">搜索应用</h2>

    <form action="search.php" method="GET" style="max-width: 600px; margin: 0 auto;">

        <div style="display: flex; gap: 10px;">

            <input type="text" 

                   name="q" 

                   value="<?php echo htmlspecialchars($searchKeyword); ?>" 

                   placeholder="输入应用名称、介绍或更新内容..." 

                   class="form-control"

                   style="flex: 1;">

            <button type="submit" class="btn">搜索</button>

        </div>

    </form>

</div>

<!-- 搜索结果 -->

<div>

    <?php if (!empty($searchKeyword)): ?>

        <h3 style="margin-bottom: 20px;">

            搜索 "<?php echo htmlspecialchars($searchKeyword); ?>" 的结果

            <span style="color: #666; font-size: 14px;">(找到 <?php echo count($searchResults); ?> 个应用)</span>

        </h3>

        <?php if (empty($searchResults)): ?>

            <div style="text-align: center; padding: 40px; background: white; border-radius: 10px;">

                <p>没有找到匹配的应用</p>

                <p style="color: #666; margin-top: 10px;">尝试使用其他关键词搜索</p>

                <a href="index.php" class="btn" style="margin-top: 15px;">查看所有应用</a>

            </div>

        <?php else: ?>

            <div class="apps-grid">

                <?php foreach ($searchResults as $app): ?>

                    <div class="app-card">

                        <?php if (!empty($app['icon'])): ?>

                            <img src="<?php echo htmlspecialchars($app['icon']); ?>" 

                                 alt="<?php echo htmlspecialchars($app['name']); ?>" 

                                 class="app-icon">

                        <?php endif; ?>

                        <div class="app-name">

                            <a href="detail.php?id=<?php echo $app['id']; ?>" 

                               style="color: inherit; text-decoration: none;">

                                <?php echo highlightKeyword(htmlspecialchars($app['name']), $searchKeyword); ?>

                            </a>

                        </div>

                        <div class="app-version">版本: <?php echo htmlspecialchars($app['version']); ?></div>

                        <div class="app-description">

                            <?php echo highlightKeyword(htmlspecialchars($app['description']), $searchKeyword); ?>

                        </div>

                        

                        <div style="display: flex; gap: 10px; margin-top: 10px;">

                            <a href="detail.php?id=<?php echo $app['id']; ?>" 

                               class="btn btn-secondary" 

                               style="flex: 1; text-align: center; padding: 8px 12px;">

                                查看详情

                            </a>

                            <a href="<?php echo htmlspecialchars($app['download_url']); ?>" 

                               class="btn" 

                               style="flex: 1; text-align: center; padding: 8px 12px;"

                               target="_blank">

                                下载

                            </a>

                        </div>

                    </div>

                <?php endforeach; ?>

            </div>

        <?php endif; ?>

    <?php else: ?>

        <div style="text-align: center; padding: 40px; background: white; border-radius: 10px;">

            <p>请输入搜索关键词</p>

        </div>

    <?php endif; ?>

</div>

<?php include 'includes/footer.php'; ?>